<?php
/***********************************************
	This class would use the PHP DOM API
	to scrape specific content.
************************************************/
class DomApi {
	private $allH1Elements = NULL;
	private $allH2Elements = NULL;
	private $allH3Elements = NULL;
	private $allH4Elements = NULL;
	
	private $h1Count = NULL;
	private $h2Count = NULL;
	private $h3Count = NULL;
	private $h4Count = NULL;
	
	private $hTagsCountArray = NULL;
	private $selectedHTag = NULL;
	private $keywordsArray = NULL;
	
	/************************************************************/
	
	function __construct($scrapedContent) {
		$this->hTagsCountArray = array();
		$this->keywordsArray = array();
		
		$domObject = new DOMDocument();
		
		/***********************************************
			This would load the scraped content into
			the DOM object.
			
			Any error would be suppressed for safety.
			
			Nevertheless, errors would definitely
			exist since the scraped content would
			have inconsistent HTML coding.
		************************************************/
		@$domObject->loadHTML($scrapedContent);
		
		$this->getAllH1Elements($domObject);
		$this->getAllH2Elements($domObject);
		$this->getAllH3Elements($domObject);
		$this->getAllH4Elements($domObject);
		
		$this->sortArray();
		$this->identifyKeywords();
	}
	
	/************************************************************/
	
	function getAllH1Elements($domObject) {
		$this->h1Count = 0;
		$this->allH1Elements = $domObject->getElementsByTagName('h1');
		
		foreach ($this->allH1Elements as $h1) {
			$this->h1Count++;
		}
		
		if ($this->h1Count > 24 || $this->h1Count < 7) {
			$this->h1Count = 0;
		}
	}
	
	/************************************************************/
	
	function getAllH2Elements($domObject) {
		$this->h2Count = 0;
		$this->allH2Elements = $domObject->getElementsByTagName('h2');
		
		foreach ($this->allH2Elements as $h2) {
			$this->h2Count++;
		}
		
		if ($this->h2Count > 24 || $this->h2Count < 7) {
			$this->h2Count = 0;
		}
	}
	
	/************************************************************/
	
	function getAllH3Elements($domObject) {
		$this->h3Count = 0;
		$this->allH3Elements = $domObject->getElementsByTagName('h3');
		
		foreach ($this->allH3Elements as $h3) {
			$this->h3Count++;
		}
		
		if ($this->h3Count > 24 || $this->h3Count < 7) {
			$this->h3Count = 0;
		}
	}
	
	/************************************************************/
	
	function getAllH4Elements($domObject) {
		$this->h4Count = 0;
		$this->allH4Elements = $domObject->getElementsByTagName('h4');
		
		foreach ($this->allH4Elements as $h4) {
			$this->h4Count++;
		}
		
		if ($this->h4Count > 24 || $this->h4Count < 7) {
			$this->h4Count = 0;
		}
	}
	
	/************************************************************/
	
	function sortArray() {
		$this->hTagsCountArray = array("h1" => $this->h1Count,
									"h2" => $this->h2Count,
									"h3" => $this->h3Count,
									"h4" => $this->h4Count
								);
		
		arsort($this->hTagsCountArray);
		reset($this->hTagsCountArray);
		$this->selectedHTag = key($this->hTagsCountArray);
	}
	
	/************************************************************/
	
	function identifyKeywords() {
		switch ($this->selectedHTag) {
			case "h1":
				if ($this->h1Count != 0) {
					foreach ($this->allH1Elements as $h1) {
						array_push($this->keywordsArray, $h1);
					}
				}
			break;
			/************************************************************/
			case "h2":
				if ($this->h2Count != 0) {
					foreach ($this->allH2Elements as $h2) {
						array_push($this->keywordsArray, $h2);
					}
				}
			break;
			/************************************************************/
			case "h3":
				if ($this->h3Count != 0) {
					foreach ($this->allH3Elements as $h3) {
						array_push($this->keywordsArray, $h3);
					}
				}
			break;
			/************************************************************/
			case "h4":
				if ($this->h4Count != 0) {
					foreach ($this->allH4Elements as $h4) {
						array_push($this->keywordsArray, $h4);
					}
				}
			break;
		}
	}
	
	/************************************************************/
	
	function getArticleKeywords() {
		return $this->keywordsArray;
	}
}
?>